package com.example.shaomengjie.smvp.net.http;

import android.content.Context;
import android.util.Log;

import com.example.shaomengjie.smvp.utils.Constant;
import com.example.shaomengjie.smvp.bean.TestBean;
import com.example.shaomengjie.smvp.net.api.ApiResponse;
import com.example.shaomengjie.smvp.net.api.ApiService;
import com.example.shaomengjie.smvp.net.cache.CacheProvider;
import com.example.shaomengjie.smvp.net.exception.ApiException;
import com.example.shaomengjie.smvp.net.parser.GsonTSpeaker;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.rx_cache2.EvictProvider;
import io.rx_cache2.internal.RxCache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpManager {
    protected static final int DEFAULT_TIMEOUT = 5;
    protected static Context mContext;
    protected volatile static HttpManager instance;
    protected OkHttpClient okHttpClient;

    private HttpManager() {
        HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
        HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("HttpManager",message);
            }
        });
        loggingInterceptor.setLevel(level);
        //拦截请求和响应日志并输出，其实有很多封装好的日志拦截插件，大家也可以根据个人喜好选择。
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .addInterceptor(loggingInterceptor);

        okHttpClient = builder.build();
    }

    public static HttpManager getInstance() {
        if (instance == null) {
            synchronized (HttpManager.class) {
                if (instance == null) {
                    instance = new HttpManager();
                }
            }
        }
        return instance;
    }

    public static void init(Context context){
        mContext=context;
    }

}
